package cn.lyf.service;

import cn.lyf.dao.CommentDao;
import cn.lyf.dao.BlogDao;
import cn.lyf.dao.ReplayDao;
import cn.lyf.entity.*;
import cn.lyf.util.DateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    BlogDao blogDao;

    @Autowired
    CommentDao commentDao;

    @Autowired
    ReplayDao replayDao;

    //评论博客
    @Override
    public Comment commentOnBlog(String blogid, Comment comment) {
        System.out.println("service blogid=="+blogid);
        //根据博客id查找博客
        Blog blog = blogDao.findOneBlog(blogid);
        System.out.println(blog);
        comment = commentDao.addComment(comment);
        List<Comment> comments = blog.getComments();
        if(comments==null){
            comments = new ArrayList<>();
            blog.setComments(comments);
        }
        comments.add(0,comment);
        blog.setCommentnum(blog.getCommentnum()+1);
        blogDao.add(blog);
        return comment;
    }

    //根据博客id查找对应的评论
    @Override
    public List<Comment> findCommentsByBlogId(String bloid) {

        return commentDao.findCommentsByBlogid(bloid);
    }

    //根据id查找评论
    @Override
    public Comment findById(String id) {
        return commentDao.findById(id);
    }

    //回复
    //commentid:评论id
    //replayid:被回复id
    @Override
    public Replay replay2Comment(String commentid,String replayid,String content,User user) {
        Comment comment = commentDao.findById(commentid);
        //如果是0就是回复评论，不是0就是回复别人的回复
        Replay replay = new Replay();
        if(!"0".equals(replayid)){
            Replay replay2 = replayDao.findById(replayid);
            replay.setNickname(user.getNickname());//设置回复别人的用户名称
            replay.setContent(content);//设置回复的内容
            replay.setNickname2(replay2.getNickname());//设置被回复人的网名
            replay.setContent2(replay2.getContent());//设置被回复的回复信息
            replay.setTcreate(DateFormat.dateFormat());
            replayDao.add(replay);
            comment.getReplays().add(replay);
            commentDao.addComment(comment);
        }else {
            replay.setNickname(user.getNickname());//设置回复别人的用户名称
            replay.setContent(content);//设置回复的内容
            replay.setNickname2(comment.getNickname());//设置被回复人的网名
            replay.setContent2(comment.getContent());//设置被回复的评论信息
            replay.setTcreate(DateFormat.dateFormat());
            replayDao.add(replay);
            List<Replay> replays = new ArrayList<>();
            replays.add(0,replay);//往头部插入
            comment.setReplays(replays);
            commentDao.addComment(comment);

        }
        return replay;

    }

    @Override
    public List<Replay> findReplaysByCommentid(String commentid) {
        Comment comment = commentDao.findById(commentid);
        return comment.getReplays();
    }

    @Override
    public void delOneComment(String blogid,Comment comment) {
        commentDao.delOneComment(comment);
        Blog blog = blogDao.findOneBlog(blogid);
        List<Comment> comments = blog.getComments();
        comments.remove(comment);
        blog.setCommentnum(blog.getCommentnum()-1);
        blogDao.add(blog);
    }
}
